<template>
  <a-modal
    destroyOnClose
    title="选择商品"
    :zIndex="zIndex"
    v-model="value"
    :width="900"
    @cancel="onCancel()"
    :footer="null"
    :closable="false"
    :maskClosable="false"
    :bodyStyle="{ maxHeight: '70vh', overflow: 'scroll' }"
  >
    <a-card>
      <a-row align="middle" style="margin-bottom: 10px">
        <a-space
          class="search-item"
        >视频名称：
          <a-input v-model="queryParam.search_key" placeholder="请填写搜索商品名称" />
        </a-space>
        <a-space class="search-item">
          <a-button @click="refresh" type="primary"> 搜索 </a-button>
        </a-space>
      </a-row>
      <a-row v-if="selectGoodsList.length > 0" type="flex" align="middle" class="img-list">
        <img
          class="selectImg"
          v-for="(goods1, index) of selectGoodsList"
          style="width: 30px; height: 30px; margin-right: 10px"
          :key="index"
          @click="delSelect(index)"
          :src="goods1.pic_url"
          alt=""
        />
      </a-row>
      <s-table ref="table" size="default" row-key="id" :columns="columns" :data="loadData">

        <template v-if="goods" slot="title" slot-scope="title, goods">
          <a-row>
            <a-space>
              <img style="width: 40px;height: 40px;" :src="goods.pic_url" alt="">
              ID{{ goods.id }}
              <span style="font-weight: bold;">{{ title }}</span>
            </a-space>
          </a-row>
        </template>
        <a-space slot="action" slot-scope="text, goods">
          <a-button @click="selectGoods(goods)" size="small">
            选择
          </a-button>
        </a-space>
      </s-table>
    </a-card>
    <a-row justify="end" type="flex" style="margin-top: 15px">
      <a-space>
        <a-button @click="onCancel"> 取消 </a-button>
        <a-button type="primary" @click="onOk"> 确认 </a-button>
      </a-space>
    </a-row>
  </a-modal>
</template>

<script>
// import { list } from '@/api/goods/goods'
import { STable } from '@/components'
// import { list as catListApi } from '@/api/goods/cat'
import { getVideoList } from '@/api/content'

const columns = [
  {
    title: '商品信息',
    dataIndex: 'title',
    scopedSlots: { customRender: 'title' }
  },
  {
    title: '操作',
    dataIndex: 'action',
    width: '40px',
    scopedSlots: { customRender: 'action' }
  }
]

export default {
  name: 'SearchGoods',
  components: {
    STable
  },
  data () {
    return {
      columns: columns,
      catList: [],
      showModal: true,
      selectGoodsList: [],
      loadData: (parameter) => {
        var newObj = {}
        Object.assign(newObj, parameter, this.queryParam)
        newObj.mch_id = this.mchId
        return getVideoList(newObj).then((res) => {
          return res.data
        })
      },
      queryParam: {
        search_key: ''
      }
    }
  },
  props: {
    value: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    //   重新获取数据
    refresh (row) {
      this.$nextTick(() => {
        this.$refs.table.refresh(true)
      })
    },
    // 选中
    selectGoods (goods) {
      var goodsInfo = goods
      for (var i = 0; i < this.selectGoodsList.length; i++) {
        if (this.selectGoodsList[i].id === goodsInfo.id) {
          return false
        }
      }
      this.selectGoodsList.push(goodsInfo)
    },
    // 删除选中
    delSelect (index) {
      this.selectGoodsList.splice(index, 1)
    },
    // 取消弹框
    onCancel () {
      this.$emit('input', false)
    },
    // 确定弹框
    onOk () {
      this.$emit('input', false)
      this.$emit('onOk', this.selectGoodsList)
    }
  }
}
</script>
<style lang="less" scoped>
.permission-form {
  /deep/ .permission-group {
    margin-top: 0;
    margin-bottom: 0;
  }
}
.search-item {
  margin-left: 10px;
}
.selectImg:hover {
  opacity: 0.4;
}
.img-list {
  margin-bottom: 10px;
  background-color: #f6ffed;
  border: 1px solid #b7eb8f;
  padding: 6px;
  position: sticky;
  top: -20px;
  z-index: 999999999;
}
</style>
